<?php
/**
* 文章表数据类
*/
class article {
    /**
    * 
    * @var array
    */
    public $table = 'article';
    public $id = null;
    //public $sql = '';
    public $data = array();  // 返回值
/***************** 以上都是 类的 变量 ************************/

    /**
     *  PHP5版本 构造函数  需要写在php4 构造函数  前 
     *
     * @access    private
     * @param     string  $tplfile  模板文件
     * @return    void
     */
    function __construct( $arr=array() )
    {

        //$this->init( $arr ); //调用初始化函数

    }
    /**
     *  兼容PHP4版本  构造函数与 类名相同
     *
     * @access    private
     * @param     string  $tplfile  模板文件
     * @return    void
     */
    function article( $arr=array() ){
        $this->__construct( $arr );
    }


    /**
     * 初始化 
     * @param  [int] $id [当前文章id]
     * @return [array]      读取成功 返回当前记录数组
     * @return [false]      读取失败 返回假
     */
    function init($id){
    	$id = intval($id);
    	$rs = $this->getRow($id);
        if($rs){
            $this->id = $id;
            $this->data = $rs; 
            return $rs;   
        }
        return false;   
    }

    
    /**
     * 从数据库中 获取一行记录  返回数组 
     * @param  $id [description]
     * @return [type]     [description]
     */
    public function getRow($id){
        $id = intval($id);
        $sql = "select a.*  , b.typename
                from `article` as a
                left join  `type` as b on a.tid = b.tid 
                where a.recycle=0 and a.id = {$id}";

        $rs = dbRow($sql);
        if( !$rs ){ return $rs ;};
        $rs['arturl'] =  $this->arturl($rs['id']);
        $rs['typeurl'] = $this->typeurl($rs['tid']);

        return $rs;
    }


    /**
     * 获取文章 url
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function arturl($id = null){
         if(!$id){$id = $this->id;}
         return arturl($id);  //arturl 写在 inc/function.php中
    }


    /**
     * 获取文章 url
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function typeurl($tid = null){
         if(!$tid){$tid = $this->data['tid'];}
         return typeurl($tid);//typeurl 写在 inc/function.php中
    }


    /**
     * 浏览量+1  支持链式写法
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function click( $id = null ){
        if(!$id){$id = $this->id;}
        $sql = "update `article` set `click` = `click` + 1  where `id` = {$id} ";
        $rs = dbQuery($sql);
        return $this; //支持链式写法
    }


    //上一篇
    public function prev($id = null, $flag = false){
        if(!$id){$id = $this->id;}
        $tid = isset( $this->data['tid'] ) ? $this->data['tid'] : 0 ;

        //$rs = $this->order(' id desc ')->where("id < '{$id}' ")->limit(0,1)->find();
        if( $tid ){
            $rs = dbRow("select * from `article` where recycle=0 and `tid` = {$tid}  and `id`<{$id} order by `id` desc limit 0,1");
        }else{
            $rs = dbRow("select * from `article` where recycle=0 and `id`<{$id} order by `id` desc limit 0,1");
        }
        
        if($flag){
            return $rs;
        }
        if(!$rs){
            return '没有了...';
        }
        $url = $this->arturl($rs['id']);
        return "<a href=\"{$url}\">{$rs['title']}</a>";
    }


    //下一篇
    public function  next($id = null, $flag = false){
        if(!$id){$id = $this->id;}
        $tid = isset( $this->data['tid'] ) ? $this->data['tid'] : 0 ;
        
        //$rs = $this->order(' id asc ')->where("id > '{$id}' ")->limit(0,1)->find();
        if( $tid ){
            $rs = dbRow("select * from `article` where recycle=0 and  `id`>{$id} and `tid` = {$tid} order by `id` asc limit 0,1");
        }else{
            $rs = dbRow("select * from `article` where recycle=0 and  `id`>{$id} order by `id` asc limit 0,1");
        }

        if($flag){
            return $rs;
        }
        if(!$rs){
            return '没有了...';
        }
        $url = $this->arturl($rs['id']);
        return "<a href=\"{$url}\">{$rs['title']}</a>";
        
    }


    //读取文章列表 arr 最好 是 数组  或者  是 tid
    public function getList( $arr=null ){
        //默认配置
        $c = array(
                'tid' =>null,//筛选 哪个分类下的文章  tid=5   //以后支持 5，8，9，7
                'limit' =>10,//个数   limit=10   limit=10，10     is_numeric()
                'orderby' => 'sorttime desc', //排序  order by sorttime desc 
        );

        //如果 参数不是数组 认为 是 tid
        if(!is_array($arr)){
            $arr = array(
                'tid' => intval($arr)
            );

        }
        //合并数组
        $c = array_merge($c,$arr);

        //如果没有 tid  取 当前的文章的 tid
        $c['tid'] = empty($c['tid'])? $this->data['tid'] : $c['tid'];



        //这个位置后期需要 优化 lrxin
        $typelist = dbList("select * from `type` where 1 order by sort asc , tid asc ");
        $tree = new tree();//类实例化为对象
        $tree->init( $typelist );//初始化数据  
        $ids =    $tree->getIds( $c['tid'] );



        $sql = "select a.*  , b.typename
                from `article` as a
                left join  `type` as b on a.tid = b.tid 
                where a.recycle=0 and a.display = 1 and a.tid in ({$ids})
                order by {$c['orderby']}
                limit {$c['limit']}
                ";
        //echo $sql; 

        $rs = dblist($sql);//从数据控中读取  二位数组 
        foreach( $rs as $k => $v){
            $v['arturl'] =  $this->arturl($v['id']);
            $v['typeurl'] = $this->typeurl($v['tid']);
            $rs[$k] = $v;
        }
        

        return $rs;

    }

    
    
}
